#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：11.使用面向对象的方式运行线程代码.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-10-31 15:17
'''
import requests
import threading

class ImageSpiderThread(threading.Thread):
    def __init__(self, url, file_name = None):
        super().__init__()
        self.url = url
        self.file_name = file_name

    # 重写线程中运行绑定任务的方法
    def run(self) -> None:
        response = requests.get(str(self.url)).content
        with open("11images/" + str(self.file_name) + ".png", mode="wb") as file:
            print(f"正在下载：{str(self.file_name)}")
            file.write(response)
            print(f"下载完成：{str(self.file_name)}")

url_list = [
    "https://t10.baidu.com/it/u=579885007,232778812&fm=30&app=106&f=JPEG?w=640&h=962&s=F6B630C0048245550E2821130100E0D0",
    "https://ww2.sinaimg.cn/mw690/008Dx8Yely1hre748fydoj30u00k0q65.jpg",
    "https://ww4.sinaimg.cn/mw690/008Dx8Yely1hrniembn9pj30gf0g8tbv.jpg",
    "https://wx1.sinaimg.cn/mw690/008Dx8Yely1hrp5nr0s79j30og0ogn08.jpg",
    "https://t11.baidu.com/it/u=288090808,240015165&fm=30&app=106&f=JPEG?w=640&h=738&s=B921C9137F564EC05265395B03007033",
    "https://ww1.sinaimg.cn/mw690/008Dx8Yely1hr0gmuf5fnj31401cl1kx.jpg",
    "https://t12.baidu.com/it/u=2580020530,232668238&fm=30&app=106&f=JPEG?w=640&h=885&s=DF08A344C8F3F38449D459430100F0B1",
    "https://t10.baidu.com/it/u=174558918,233266531&fm=30&app=106&f=JPEG?w=640&h=962&s=AD84D210C71051D4D4A11D450100A0B0"
]

if __name__ == '__main__':
    name = 1
    print(len(url_list))
    for url in url_list:
        imageSpiderThread = ImageSpiderThread(url, name)
        imageSpiderThread.start()
        name+=1